<template>
	<div ref="scheduleShiftProductionChart" v-resize="resizeChart" style="width: 100%;height: 100%;"></div>
</template>

<script>

    import * as echarts from 'echarts';
    import { color, borderColor, legendTextStyle } from './chart-conf';
    import homeMixin from '../../../../../mixins/dom-resize';
    import { reportProductionHomeDataProductCurrentShiftYield } from '../../../../../api/production/kanBan';
    export default {
        name: 'ScheduleShiftProductionChart',
        props: {
            homeData: {
                type: Object,
                default() {
                    return {};
                }
            },
            workshopId: {
                type: Number,
                default: null
            },
            refreshData: {
                type: [Number, String],
                default: ''
            },
            processCode: {
                type: String,
                default: 'XS'
            }
        },
        mixins: [homeMixin],
        data() {
            return {
                example: null,
                borderColor: '#c5c8ce',
                option: {
                    title: {
                        text: '',
                        textStyle: {
                            fontWeight: 'none'
                        }
                    },
                    tooltip: {
                        trigger: 'axis'
                    },
                    grid: {
                        top: '30px',
                        right: '10px',
                        bottom: '10px',
                        left: '20px',
                        containLabel: true
                    },
                    legend: {
                        data: ['标准产量(kg)', '实际产量(kg)', '亏欠产量(kg)'],
                        textStyle: {
                            color: legendTextStyle
                        }
                    },
                    xAxis: {
                        type: 'category',
                        data: [323, 242, 355, 276, 118, 434, 251],
                        axisLabel: {
                            show: true,
                            color: color

                        },
                        nameTextStyle: {
                            color: 'red'
                        },
                        boundaryGap: true,
                        axisLine: {
                            lineStyle: {
                                color: borderColor
                            }
                        }
                    },
                    yAxis: {
                        type: 'value',
                        name: 'KG',
                        nameLocation: 'end',
                        nameTextStyle: {
                            align: 'right'
                        },
                        splitLine: {
                            lineStyle: {
                                color: 'none'
                            }
                        },
                        axisLabel: {
                            show: true,
                            color: color

                        },
                        axisLine: {
                            lineStyle: {
                                color: borderColor
                            }
                        }
                    },
                    series: [
                        {
                            name: '标准产量(kg)',
                            type: 'bar',
                            data: [],
                            label: {
                                show: true,
                                position: 'outside',
                                borderType: 'none',
                                color: '#808695',
                                textBorderColor: 'none'
                            },
                            smooth: true,
                            barGap: 0,
                            symbol: 'none',
                            itemStyle: {
                                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                                    { offset: 0, color: '#A15CFD' },
                                    { offset: 0.5, color: '#A15CFD' },
                                    { offset: 1, color: '#A15CFD' }
                                ])
                            },
                            emphasis: {
                                itemStyle: {
                                    color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                                        { offset: 0, color: '#A15CFD' },
                                        { offset: 0.5, color: '#A15CFD' },
                                        { offset: 1, color: '#A15CFD' }
                                    ])
                                }
                            }
                        },
                        {
                            name: '实际产量(kg)',
                            type: 'bar',
                            data: [],
                            label: {
                                show: true,
                                position: 'outside',
                                borderType: 'none',
                                color: '#808695',
                                textBorderColor: 'none'
                            },
                            smooth: true,
                            symbol: 'none',
                            barGap: 0,
                            itemStyle: {
                                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                                    { offset: 0, color: '#E04D60' },
                                    { offset: 0.5, color: '#E04D60' },
                                    { offset: 1, color: '#E04D60' }
                                ])
                            },
                            emphasis: {
                                itemStyle: {
                                    color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                                        { offset: 0, color: '#E04D60' },
                                        { offset: 0.5, color: '#E04D60' },
                                        { offset: 1, color: '#E04D60' }
                                    ])
                                }
                            }
                        },
                        {
                            name: '亏欠产量(kg)',
                            type: 'bar',
                            data: [],
                            label: {
                                show: true,
                                position: 'outside',
                                borderType: 'none',
                                color: '#808695',
                                textBorderColor: 'none'
                            },
                            smooth: true,
                            symbol: 'none',
                            barGap: 0,
                            itemStyle: {
                                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                                    { offset: 0, color: '#2d8cf0' },
                                    { offset: 0.5, color: '#2d8cf0' },
                                    { offset: 1, color: '#2d8cf0' }
                                ])
                            },
                            emphasis: {
                                itemStyle: {
                                    color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                                        { offset: 0, color: '#2d8cf0' },
                                        { offset: 0.5, color: '#2d8cf0' },
                                        { offset: 1, color: '#2d8cf0' }
                                    ])
                                }
                            }
                        }
                    ]
                },
                chartData: []
            };
        },
        methods: {
            resize() {
                window.addEventListener('resize', () => {
                    this.resizeChart();
                });
            },
            initChart() {
                this.$nextTick(() => {
                    this.example = echarts.init(this.$refs.scheduleShiftProductionChart);
                    this.updateChart();
                });
            },
            updateChart() {
                if (this.example) {
                    this.$nextTick(() => {
                        this.option.xAxis.data = this.chartData.map(x => x.ifCurrentShift ? `${x.shiftName}(当班)` : `${x.shiftName}(上一班)`);
                        this.option.series[0].data = this.chartData.map(x => x.theoryQty);
                        this.option.series[1].data = this.chartData.map(x => x.shiftQty);
                        this.option.series[2].data = this.chartData.map(x => x.owingQty);
                        this.example.setOption(this.option);
                    });
                } else {
                    this.initChart();
                }
            },
            getDataRequest() {
                if (!this.workshopId) return false;
                // console.log('班次产量')
                return reportProductionHomeDataProductCurrentShiftYield({
                    workshopId: this.workshopId,
                    processCode: this.processCode
                }).then(res => {
                    if (res.data.status === 200) {
                        this.chartData = res.data.res ? res.data.res.map(x => {
                            x.theoryQty = x.theoryQty ? parseInt(x.theoryQty) : 0;
                            x.shiftQty = x.shiftQty ? parseInt(x.shiftQty) : 0;
                            x.owingQty = x.owingQty ? parseInt(x.owingQty) : 0;
                            return x;
                        }) : [];
                        this.updateChart();
                    }
                });
            }
        },
        mounted() {
            this.resize();
            this.initChart();
        },
        watch: {
            refreshData: {
                handler() {
                    this.getDataRequest();
                },
                deep: true,
                immediate: true
            }
        }
    };
</script>

<style scoped lang="scss"></style>
